package com.health.controller;

import com.health.pojo.TableColumn;
import com.health.pojo.Users;
import com.health.service.HealthComboService;
import com.health.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

// 查询套餐下拉列表选中的体检项
@WebServlet("/searchDataByCombo")
public class SearchDataByComboServlet extends HttpServlet {
    UserService userService = new UserService();
    HealthComboService healthComboService = new HealthComboService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TableColumn tableColumn;
        List<Users> usersListByItem = new ArrayList<>();
        int type = Integer.parseInt(request.getParameter("type"));
        // 列名判断
        if (type == 1) {
            tableColumn = new TableColumn("套餐编号", "套餐名", "日期", "用户Id");
        } else {
            tableColumn = new TableColumn("套餐编号", "套餐名", "项目编号", "项目名");
            String userId = request.getParameter("userId");     // 获取选中的userId
            String examineId = request.getParameter("examineId"); //获取选中的examineId
            String value = request.getParameter("value");//获取value的值
            String examineDate = value.split("---")[1];//提取value值中的日期
            System.out.println("value" + examineDate);
            usersListByItem = userService.searchDataByItem(Integer.parseInt(userId), Integer.parseInt(examineId), examineDate);//传入获取参数查询item小项
            request.setAttribute("userId", userId);      // 存入用户Id用于第二次下拉框请求
            request.setAttribute("examineDate", examineDate.replace("-", ""));
            request.setAttribute("usersListByCombo", usersListByItem);
        }
        // 获取userId回显套餐下拉信息
        List<Users> usersListByCombo = userService.searchDataByCombo(Integer.parseInt(request.getParameter("userId")));
        // 获取用户下拉框的回显下拉信息
        List<Users> usersList = userService.searchUserInformation();
        //
        // 设置请求域
        request.setAttribute("column", tableColumn);
        request.setAttribute("usersListByCombo", usersListByCombo);
        request.setAttribute("usersList", usersList);
        request.setAttribute("usersListByItem", usersListByItem);
        // 设置切换体检项表格标志位
        Integer attributeTableStatus = 1;
        request.setAttribute("attributeTableStatus", attributeTableStatus);
        // index:设置用户的下拉锁定索引
        request.setAttribute("index", request.getParameter("index"));

        /// index1:设置套餐的下拉锁定索引
        request.setAttribute("index1", request.getParameter("index1"));
        // 设置侧边栏状态
        request.setAttribute("asideStatus", 1);
        request.setAttribute("openInputView", -1);
        request.getRequestDispatcher("restrictedPages/admin.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
